package com.smartleanx.module.hrm.service.esign.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.module.hrm.dal.mysql.esign.HrmEContractCodeMapper;
import com.smartleanx.module.hrm.dal.mysql.esign.HrmEContractTempMapper;
import com.smartleanx.module.hrm.enums.ErrorCodeConstants;
import com.smartleanx.module.hrm.service.esign.ESignUrlConstant;
import com.smartleanx.module.hrm.service.esign.eums.ActorTypeEnum;
import com.smartleanx.module.hrm.service.esign.eums.FolderTypeEnum;
import com.smartleanx.module.hrm.service.esign.service.*;
import com.smartleanx.module.hrm.service.esign.temp.param.HrmEContractTempFileParam;
import com.smartleanx.module.hrm.service.esign.temp.param.HrmEContractTempParam;
import com.smartleanx.module.hrm.service.esign.temp.po.*;
import com.smartleanx.module.hrm.service.esign.temp.result.HrmContractCodeResult;
import com.smartleanx.module.hrm.service.esign.temp.result.HrmEContractTempDetailResult;
import com.smartleanx.module.hrm.service.esign.temp.result.HrmEContractTempResult;
import com.smartleanx.module.hrm.service.esign.temp.vo.HrmEContractTempActorsVo;
import com.smartleanx.module.hrm.service.esign.temp.vo.HrmEContractTempVo;
import com.smartleanx.module.hrm.util.esign.ESignUtil;
import com.smartleanx.module.hrm.util.esign.EsignHttpResponse;
import com.smartleanx.module.hrm.util.esign.enums.EsignRequestType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * e签报合同模板Service实现
 *
 * @author xzq
 * @since 2025-07-07 15:56:14
 */
@Service
public class HrmEContractCodeServiceImpl extends ServiceImpl<HrmEContractCodeMapper, HrmEContractCode> implements HrmEContractCodeService {
    @Value("${esign.url}")
    private String esginUrl;
    @Autowired
    private HrmEContractTempMapper hrmEContractTempMapper;
    @Autowired
    private HrmECustomComponentsService hrmECustomComponentsService;
    @Autowired
    private HrmEFolderTempService hrmEFolderTempService;
    @Autowired
    private HrmETempFileService hrmETempFileService;
    @Autowired
    private HrmEContractTempActorsService hrmEContractTempActorsService;


    @Override
    public List<HrmEContractCode> getContractCodeBytempId(Long tempId) {
        return list(new LambdaQueryWrapper<HrmEContractCode>()
        .eq(HrmEContractCode::getDataStatus,1)
        .eq(HrmEContractCode::getTempId,tempId)
        .orderByAsc(HrmEContractCode::getSortNo));
    }

    @Override
    public boolean saveBathCode(List<HrmContractCodeResult> param,Long tempId) {
        List<HrmEContractCode> collect = param.stream().map(p -> {
            HrmEContractCode hrmEContractCode = new HrmEContractCode();
            BeanUtil.copyProperties(p, hrmEContractCode);
            hrmEContractCode.setDataStatus(1);
            hrmEContractCode.setTempId(tempId);
            hrmEContractCode.setName(p.getCodeName());
            return hrmEContractCode;
        }).collect(Collectors.toList());
        return saveBatch(collect);
    }

    @Override
    public boolean removeContractCode(Long tempId) {
        return remove(new LambdaQueryWrapper<HrmEContractCode>()
        .eq(HrmEContractCode::getTempId,tempId));
    }
}
